package com.weetop.cfw.mine.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.TimeUtils
import com.weetop.cfw.R
import com.weetop.cfw.base.presenter.imp.SuccessfulPaymentPresenterImp
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.base.view.SuccessfulPaymentView
import com.weetop.cfw.bean.AudioVideoDetailBean
import com.weetop.cfw.constants.UrlConstants
import kotlinx.android.synthetic.main.activity_successful_payment.*
import java.util.*

/**
 * 支付成功界面
 */
class SuccessfulPaymentActivity : CommonBaseActivity(),View.OnClickListener,SuccessfulPaymentView {

    private lateinit var successfulPaymentPresenterImp: SuccessfulPaymentPresenterImp

    companion object{
        private const val INFO_ID = "INFO_ID"
        private const val ORDER_ID = "ORDER_ID"
        fun startSuccessfulPaymentActivity(context: Context,inforId:Int,orderId:String){
            val intent = Intent(context,SuccessfulPaymentActivity::class.java)
            intent.putExtra(INFO_ID,inforId)
            intent.putExtra(ORDER_ID,orderId)
            ActivityUtils.startActivity(intent)
        }
    }

    override fun getLayoutId() = R.layout.activity_successful_payment

    override fun initView(savedInstanceState: Bundle?) {
         setViewsOnClickListener(this,imageSuccessfulPaymentBack,btnToLearn)
    }

    override fun initData(savedInstanceState: Bundle?) {
        successfulPaymentPresenterImp = SuccessfulPaymentPresenterImp()
        successfulPaymentPresenterImp.attachView(this)

        successfulPaymentPresenterImp.getCourseDetail(this,intent.getIntExtra(INFO_ID,0))

        textOrderNo.text = "订单号：${intent.getStringExtra(ORDER_ID)}"
        textPlaceAnOrder.text = TimeUtils.date2String(Date(System.currentTimeMillis()),"yyyy-MM-dd HH:mm")
    }

    override fun onClick(v: View?) {
       when(v?.id){
           R.id.imageSuccessfulPaymentBack -> ActivityUtils.finishActivity(this)
           R.id.btnToLearn -> CourseDetailActivity.startCourseDetailActivity(this,intent.getIntExtra(INFO_ID,0),0)
       }
    }

    override fun onDestroy() {
        super.onDestroy()
        successfulPaymentPresenterImp.detachView()
    }

    /**
     * 课程详情数据获取成功
     */
    override fun courseDetailGetSuccess(audioVideoDetailBean: AudioVideoDetailBean) {
        imageCourseLogo.setUrlImage("${UrlConstants.baseUrl}${audioVideoDetailBean.imgurl}")
        textCourseTeacherName.text = audioVideoDetailBean.author
        textCoursePlayNumber.text = audioVideoDetailBean.playnum
        textCourseTitle.text = audioVideoDetailBean.title
    }
}
